package com.glsc.ngateway.opmanage.service.deposit;

import cn.hutool.core.collection.CollectionUtil;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.oracle.xinyizj.FsAccBankInfo;
import com.glsc.ngateway.common.base.repo.oracle.xinyizj.FsAccBankInfoRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class FsAccBankInfoService {


    @Resource
    private FsAccBankInfoRepository fsAccBankInfoRepository;

    /**
     * 根据银行账户查询信息
     * @param bankAcc
     * @return
     */
    public FsAccBankInfo findByBankAcc(String bankAcc) {
        if (StringUtils.isEmpty(bankAcc)) {
            return null;
        }
        List<FsAccBankInfo> infos = fsAccBankInfoRepository.findAll(Example.of(FsAccBankInfo.builder().bankAcc(bankAcc).build()));
        if (CollectionUtil.isEmpty(infos)) {
            return null;
        }
        return infos.get(0);
    }


    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<FsAccBankInfo> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "bankAcc";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<FsAccBankInfo> page = fsAccBankInfoRepository.findAll(SpecificationUtil.buildSpecification(param, FsAccBankInfo.class), pageable);

        return page;
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<FsAccBankInfo> listAll() {
        List<FsAccBankInfo> list = fsAccBankInfoRepository.findAll(Example.of(FsAccBankInfo.builder().build()));
        return list;
    }


    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public FsAccBankInfo findById(String id) {
        Optional<FsAccBankInfo> optional = fsAccBankInfoRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }


}
